/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.business.security.entity;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import meteocal.business.entity.Event;
import meteocal.business.security.control.PasswordEncrypter;

/**
 *
 * @author luana
 */
@Entity(name ="USERS")
@NamedQueries({
        @NamedQuery(name=User.FIND_USER_BY_USERNAME, query = "SELECT u FROM USERS u where u.username LIKE :username "),
        @NamedQuery(name=User.FIND_USER_BY_NAME, query = "SELECT u FROM USERS u where u.name LIKE :name"),
        @NamedQuery(name=User.FIND_ALL, query="SELECT u FROM USERS u ORDER BY u.username ASC"),
        @NamedQuery(name = User.FIND_USER_BY_CITY, query = "SELECT u FROM USERS u WHERE u.city LIKE :city "),
})
public class User implements Serializable {
    public static final String FIND_USER_BY_USERNAME = "User.FindUserByUsername";
    public static final String FIND_USER_BY_NAME = "User.FindUserByName";
    public static final String FIND_ALL = "User.FindAll";
    public static final String FIND_USER_BY_CITY = "User.FindUserByCity";
    
    /**
     * Join Table PARTICIPANTS which relates an event to all the user who are going to partecipate to it
     */
  
    @ManyToMany(mappedBy = "partecipatingUsers")
    private Collection<Event> eventToPartecipate;
    
    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "pendingUsers")
    private Collection<Event> eventInPending;
    

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "creator")
    private Collection<Event> eventCreated;
    
    private static final long serialVersionUID = 1L;
    
    /**
     * The name of the key field "alias" , so named in the RASD and DD, has been
     * changed to "username" because alias is a reserved SQL-99 reserved
     * keyword
     */
    
    @Id
    @NotNull(message = "Must not be empty")
    private String username;
    
    @NotNull(message = "Must not be empty")
    private String password;
    
    
    @NotNull
    private String groupname;
    
    
    @NotNull(message = "Must not be empty")
    @Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
             message = "Invalid email address")
    private String email;
    
    @NotNull(message = "Must not be empty")
    private String name;
    
    
    private String city;
    
    @Pattern(regexp="([\\d]{3})-([\\d]{7})",
            message="Invalid phone number; Only Mobile Numbers are accepted (3+7 digits)")
    private String phonenumber;
    
    
    private String avatar; 
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "PRIVACY_LEVEL")
    private boolean privacyLevel;
    
    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = PasswordEncrypter.encryptPassword(password);
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getGroupname() {
        return groupname;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public String getCity() {
        return city;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public String getAvatar() {
        return avatar;
    }

    public boolean isPrivacyLevel() {
        return privacyLevel;
    }

    public void setPrivacyLevel(boolean privacyLevel) {
        this.privacyLevel = privacyLevel;
    }

    
   
    public Collection<Event> getEventToPartecipate() {
        return eventToPartecipate;
    }

    public void setEventToPartecipate(Collection<Event> eventCollection) {
        this.eventToPartecipate = eventCollection;
    }

    
    public Collection<Event> getEventCreated() {
        return eventCreated;
    }

    public void setEventCreated(Collection<Event> eventCreated) {
        this.eventCreated = eventCreated;
    }

    public Collection<Event> getEventInPending() {
        return eventInPending;
    }

    public void setEventInPending(Collection<Event> eventInPending) {
        this.eventInPending = eventInPending;
    }

    public User() {
    }

   
    public User(String username, String city, String email, String groupname, String name, String password) {
        this.username = username;
        this.city = city;
        this.email = email;
        this.groupname = groupname;
        this.name = name;
        this.password = password;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if( getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if(!username.equals(other.username))
            return false;
        return true;
    }
    

}